package com.har.modules.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.har.common.exception.RRException;
import com.har.common.utils.AESUtil;
import com.har.common.utils.R;
import com.har.common.utils.ValidatorUtils;
import com.har.modules.web.entity.ResetEntity;
import com.har.modules.web.entity.UserBackEntity;
import com.har.modules.web.entity.UserOrCompanyEntity;
import com.har.modules.web.service.CompanyService;
import com.har.modules.web.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 * 企业员工
 *
 * @author maorui
 *
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CompanyService companyService;

    /**
     * 忘记密码
     * @param resetEntity
     * @return
     */
    @RequestMapping("/reset")
    public R reset(@RequestBody ResetEntity resetEntity) throws ParseException {
        //类型为0 修改企业员工用户
        if(resetEntity.getType()==0){
            userService.update(resetEntity);
        }
        //类型1 修改企业账号
        if(resetEntity.getType()==1){
            companyService.update(resetEntity);
        }
        return R.ok();
    }


    /**
     * 修改账号信息
     * @param user
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserOrCompanyEntity user,HttpServletRequest request) throws Exception {
        Cookie[] cookies=request.getCookies();
        String data=null;
        JSONObject respJson=null;
        for (Cookie cookie:cookies){
            if(null!=cookie){
                if(cookie.getName().equals("login")){
                    System.out.println(cookie.getValue());
                    data = AESUtil.getInstance().decrypt(cookie.getValue());
                    respJson = JSONObject.parseObject(data);
                }
                log.info("respJson>>>{}",respJson);
            }
        }
        if(data==null){
            throw new RRException("请先登录或注册");
        }
        //用户类型 1:企业 0:员工
        Integer type=Integer.parseInt(respJson.getString("type"));
        Long id=Long.valueOf(respJson.getString("id"));
        user.setId(id);
        ValidatorUtils.validateEntity(user);
        if(0==type){
            userService.updateOfBack(user);
        }
        if(1==type){
            companyService.updateOfBack(user);
        }
        return R.ok();
    }

    /**
     * 员工账号详细信息
     */
    @RequestMapping("/info")
    public R info(HttpServletRequest request) throws Exception {
        Cookie[] cookies=request.getCookies();
        String data=null;
        JSONObject respJson=null;
        for (Cookie cookie:cookies){
            if(null!=cookie){
                if(cookie.getName().equals("login")){
                    System.out.println(cookie.getValue());
                    data = AESUtil.getInstance().decrypt(cookie.getValue());
                    respJson = JSONObject.parseObject(data);
                }
                log.info("respJson>>>{}",respJson);
            }
        }
        if(data==null){
            throw new RRException("查询失败");
        }
        Long id=Long.valueOf(respJson.getString("id"));
        Integer type=Integer.parseInt(respJson.getString("type"));
        UserBackEntity user=new UserBackEntity();
        //用户类型 1:企业 0:员工
        if(1==type){
            user = companyService.selectById(id);
            return R.ok().put("data", user);
        }
        if(0==type){
            user = userService.selectById(id);
            return R.ok().put("data", user);
        }


        return R.ok();
    }


    /**
     *  通过请求查询返回参数
     * @param request
     * @return
     */
    @RequestMapping("/queryback")
    public R queryback(HttpServletRequest request) throws Exception {
        Cookie [] cookies=request.getCookies();
        String data=null;
        JSONObject respJson=null;
        if(null!=cookies){
            for (Cookie cookie:cookies){
                if(cookie.getName().equals("login")){
                    System.out.println(cookie.getValue());
                    data = AESUtil.getInstance().decrypt(cookie.getValue());
                    respJson = JSONObject.parseObject(data);
                }
                log.info("respJson>>>{}",respJson);
            }
            if(data==null){
                throw new RRException("查询失败");
            }
            Long id=Long.valueOf(respJson.getString("id"));
            String userName=respJson.getString("userName");
            String mobile=respJson.getString("mobile");
            Integer type=Integer.parseInt(respJson.getString("type"));
            //用户类型 1:企业 0:员工
            if(1==type){
                return R.ok().put("data",companyService.queryCompany(id,mobile));
            }
            if(0==type){
                return R.ok().put("data",userService.queryUser(id,mobile));
            }
        }
        return R.error("查询失败");
    }

}
